<script setup>
import searchNav from './components/searchNav.vue'
import enterList from './components/enterList.vue'
import payList from './components/payList.vue'
import { Nothing } from '@/components/common/index.js'
import withdrawList from './components/withdrawList.vue'
import { List } from 'vant'
import { ref, onMounted } from 'vue'
import commonModel from '@/http/modules/common.js'
const page = ref(1)
const loading = ref(true)
const finished = ref(true)
const searchInfo = ref({})
const enterArray = ref([])
const withdrawArray = ref([])
const payArray = ref([])
let type = ref('enter')

function getList() {
  if (type.value === 'enter') {
    getEnterList()
  } else if (type.value === 'withdraw') {
    getWithdrawList()
  } else {
    getPayList()
  }
}
//  获取付款列表
const getPayList = () => {
  loading.value = true
  finished.value = false
  commonModel
    .gatewayApi({
      service: 'cb_withdraw_order_page',
      bizContent: {
        pageNo: page.value,
        pageSize: 10,
        tradeType: 21,
        currency: searchInfo.value.currencyId,
        statusList: searchInfo.value.status
          ? searchInfo.value.status.split(',')
          : '',
        gmtCreateBegin: searchInfo.value.noticeTimeStartTime
          ? `${searchInfo.value.noticeTimeStartTime} 00:00:00`
          : null,
        gmtCreateEnd: searchInfo.value.noticeTimeEndTime
          ? `${searchInfo.value.noticeTimeEndTime} 23:59:59`
          : null,
      },
      version: '1.0.0',
    })
    .then((result) => {
      if (result) {
        if (result.dataList && result.dataList.length) {
          if (page.value === 1) {
            payArray.value = result.dataList
          } else {
            payArray.value = payArray.value.concat(result.dataList)
          }
          if (payArray.value.length >= result.totalCount) {
            finished.value = true
          }
          loading.value = false
        } else {
          finished.value = true
          loading.value = false
        }
      }
    })
}
// 获取入账列表
function getEnterList() {
  loading.value = true
  finished.value = false
  // tradeModel
  //   .getEnterList({
  //     pageNo: page.value,
  //     pageSize: 10,
  //     ...searchInfo.value,
  //   })
  commonModel
    .gatewayApi({
      service: 'cb_trade_incoming_page',
      bizContent: {
        pageNo: page.value,
        pageSize: 10,
        currency: searchInfo.value.currencyId,
        gmtCreateBegin: searchInfo.value.noticeTimeStartTime
          ? `${searchInfo.value.noticeTimeStartTime} 00:00:00`
          : null,
        gmtCreateEnd: searchInfo.value.noticeTimeEndTime
          ? `${searchInfo.value.noticeTimeEndTime} 23:59:59`
          : null,
        auditStatusList: searchInfo.value.status
          ? searchInfo.value.status.split(',')
          : '',
        keyword: searchInfo.value.keyword
        // status: 'ToBeAssociatedWithContract,ToBeFollowedUp,Auditing', //展示待关联订单、待跟进、审核中状态的入账流水
      },
      version: '1.0.0',
    })
    .then((res) => {
      loading.value = false
      if (res && res.dataList) {
        if (page.value === 1) {
          enterArray.value = res.dataList
        } else {
          enterArray.value = enterArray.value.concat(res.dataList)
        }
        if (enterArray.value.length >= res.totalCount) {
          finished.value = true
        }
      }
    })
}

// 获取提现列表
function getWithdrawList() {
  loading.value = true
  finished.value = false
  commonModel
    .gatewayApi({
      service: 'cb_withdraw_order_page',
      bizContent: {
        pageNo: page.value,
        pageSize: 10,
        tradeType: 20,
        currency: searchInfo.value.currencyId,
        statusList: searchInfo.value.status
          ? searchInfo.value.status.split(',')
          : '',
        gmtCreateBegin: searchInfo.value.noticeTimeStartTime
          ? `${searchInfo.value.noticeTimeStartTime} 00:00:00`
          : null,
        gmtCreateEnd: searchInfo.value.noticeTimeEndTime
          ? `${searchInfo.value.noticeTimeEndTime} 23:59:59`
          : null,
      },
      version: '1.0.0',
    })
    .then((result) => {
      if (result) {
        if (result.dataList && result.dataList.length) {
          if (page.value === 1) {
            withdrawArray.value = result.dataList
          } else {
            withdrawArray.value = withdrawArray.value.concat(result.dataList)
          }
          if (withdrawArray.value.length >= result.totalCount) {
            finished.value = true
          }
          loading.value = false
        } else {
          finished.value = true
          loading.value = false
        }
      }
    })
}

function handleSearch(data) {
  searchInfo.value = data
  withdrawArray.value = []
  enterArray.value = []
  payArray.value = []
  page.value = 1
  getList()
}

function onLoad() {
  page.value++
  getList()
}

// 切换类型
function handleType(data) {
  type.value = data
  withdrawArray.value = []
  enterArray.value = []
  payArray.value = []
  page.value = 1
  searchInfo.value = {}
  getList()
}
onMounted(() => {
  getList()
})
</script>
<template>
  <search-nav :active="type" @change="handleType" @search="handleSearch" />
  <List
    v-model:loading="loading"
    :finished="finished"
    :finished-text="
      !withdrawArray.length && !enterArray.length && !payArray.length
        ? ''
        : '没有更多了'
    "
    @load="onLoad"
  >
    <enter-list v-if="type === 'enter'" :list="enterArray" />
    <withdraw-list v-else-if="type === 'withdraw'" :list="withdrawArray" />
    <pay-list v-else :list="payArray" />
    <Nothing
      v-if="
        !withdrawArray.length &&
        !enterArray.length &&
        !payArray.length &&
        !loading
      "
    />
  </List>
</template>

<style scoped lang="scss">
.van-list {
  padding: 0.1rem 0.12rem;
}
</style>
